﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor
{
    class CommandDefinitions: IXmlBuildable, IChildDeletable
    {
        public List<CommandDefinition> Commands;

        public CommandDefinitions()
        {
            this.Commands = new List<CommandDefinition>();
        }

        public CommandDefinitions(XmlNode node):this()
        {
            try
            {
                foreach (XmlNode cmdXmlNode in node.SelectNodes("CommandDefinition"))
                {
                    CommandDefinition command = new CommandDefinition(cmdXmlNode);
                    this.Commands.Add(command);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode ribbonDiffXmlNode)
        {
            try
            {
                XmlNode commandDefinitionsXmlNode = XmlHelper.AddNode(ribbonDiffXmlNode, "CommandDefinitions");

                foreach (CommandDefinition command in this.Commands)
                {
                    command.AddNode(commandDefinitionsXmlNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode commandDefinitionsNode = new TreeNode("CommandDefinitions");
                commandDefinitionsNode.ContextMenuStrip = this.BuildContextMenu(commandDefinitionsNode);
                commandDefinitionsNode.Tag = this;


                foreach (CommandDefinition command in this.Commands)
                {
                    command.BuildTreeViewElement(commandDefinitionsNode);
                }

                parentNode.Nodes.Add(commandDefinitionsNode);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private ContextMenuStrip BuildContextMenu(TreeNode relatedNode)
        {
            ContextMenuStrip cms = new ContextMenuStrip();
            cms.Tag = relatedNode;
            
            ToolStripMenuItem tsmi = new ToolStripMenuItem("Add Command Definition");
            tsmi.Click += new EventHandler(tsmi_Click);
            cms.Items.Add(tsmi);

            ContextMenuStripHelper.AddDisplayXmlMenu(cms, true);

            return cms;
        }

        void tsmi_Click(object sender, EventArgs e)
        {
            try
            {
                TreeNode selectedNode = (TreeNode)((ToolStripItem)sender).GetCurrentParent().Tag;

                CommandDefinition cmdDef = new CommandDefinition();
                cmdDef.BuildTreeViewElement(selectedNode);

                selectedNode.Expand();

                this.Commands.Add(cmdDef);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IChildDeletable Members

        public void DeleteChild(object o)
        {
            try
            {
                if (o is CommandDefinition)
                {
                    this.Commands.Remove((CommandDefinition)o);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
